package com.nuoniu.sibanyun.controller.so;


import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.dto.BarcodeMainInfoSearchDto;
import com.nuoniu.sibanyun.entity.so.VAreaCalculationDetail;
import com.nuoniu.sibanyun.entity.so.VAreaCalculationMain;
import com.nuoniu.sibanyun.entity.vo.ns_staristics.ProjectExecutionStatisticsVo;
import com.nuoniu.sibanyun.entity.vo.ns_staristics.dto.ProjectExecutionStatisticsDto;
import com.nuoniu.sibanyun.service.so.IVAreaCalculationDetailService;
import com.nuoniu.sibanyun.service.so.IVAreaCalculationMainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.nuoniu.sibanyun.common.BaseController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 面积计算-主表 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-10-22
 */
@RestController
@RequestMapping("/so/areaCalculation")
@Api(tags = "面积计算管理")
public class VAreaCalculationMainController extends BaseController {

    @Autowired
    private IVAreaCalculationMainService areaCalculationMainService;
    @Autowired
    private IVAreaCalculationDetailService areaCalculationDetailService;


    /**
     * 保存
     */
    @PostMapping("save")
    @ApiOperation("保存")
    public HttpResult save(@RequestBody VAreaCalculationMain areaCalculationMain) {
        if(areaCalculationMain == null || areaCalculationMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("明细不能为空");
        }
        areaCalculationMainService.saveAreaCalculationMain(areaCalculationMain);
        return HttpResult.ok();
    }

    @PostMapping("setPrice")
    @ApiOperation("挂价")
    public HttpResult setPrice(@RequestBody VAreaCalculationMain areaCalculationMain) {
        if(areaCalculationMain == null || areaCalculationMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("明细不能为空");
        }
        areaCalculationMainService.setPrice(areaCalculationMain);
        return HttpResult.ok();
    }

    @PostMapping("importPrice")
    @ApiOperation("导入挂价")
    public HttpResult importPrice(Integer orderId, MultipartFile file) throws IOException {
        if(orderId == null) {
            throw new NuoNiuException("请选择要挂价的面积计算表");
        }

        if(file == null) {
            throw new NuoNiuException("请选择文件");
        }

        areaCalculationMainService.importPrice(orderId, file);
        return HttpResult.ok();
    }

    /**
     * 条码解析
     */
    @ApiOperation("条码解析")
    @GetMapping("analysisByBarCode")
    public HttpResult analysisByBarCode(BarcodeMainInfoSearchDto barcodeMainInfoSearchDto) {
        return HttpResult.ok(areaCalculationDetailService.analysisByBarCode(barcodeMainInfoSearchDto));
    }

    /**
     * 获取单据编码
     */
    @ApiOperation("获取单据编码")
    @GetMapping("getCode")
    public HttpResult getCode() {
        return HttpResult.ok(areaCalculationMainService.getCode());
    }

    @DeleteMapping("delete")
    @ApiOperation("删除单据")
    public HttpResult delete(Long id) {
        areaCalculationMainService.deleteById(id);
        return HttpResult.ok();
    }

    @GetMapping("examine")
    @ApiOperation("单据审核/弃审")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        areaCalculationMainService.examine(id,status);
        return HttpResult.ok();
    }


    @ApiOperation("导入数据")
    @PostMapping("importData")
    public HttpResult<VAreaCalculationMain> importData(MultipartFile file) throws IOException, NoSuchFieldException {
        if(file == null) {
            throw new NuoNiuException("请选择文件");
        }

        return HttpResult.ok(areaCalculationMainService.importData(file));
    }

    @ApiOperation("导出数据")
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response, Integer orderId) {
        if(orderId == null) {
            throw new NuoNiuException("面积计算表不存在");
        }

        areaCalculationMainService.exportData(response, orderId);
    }

    /**
     * 统计项目 完成情况
     */
    @ApiOperation("统计项目 完成情况")
    @GetMapping("projectExecutionStatistics")
    public HttpResult<ProjectExecutionStatisticsVo> projectExecutionStatistics(ProjectExecutionStatisticsDto projectExecutionStatisticsDto) {
        return HttpResult.ok(areaCalculationMainService.projectExecutionStatistics(projectExecutionStatisticsDto));
    }



    /**
     * 统计 批次 完成情况
     */
    @ApiOperation("统计 批次 完成情况")
    @GetMapping("areaCalculationMainStatistics")
    public HttpResult<VAreaCalculationMain> areaCalculationMainStatistics(Integer projectId) {
        return HttpResult.ok(areaCalculationMainService.areaCalculationMainStatistics(projectId));
    }


    @ApiOperation("统计 单个批次 完成情况")
    @GetMapping("getBatchExecutionStatisticsVos")
    public HttpResult<VAreaCalculationMain> getBatchExecutionStatisticsVos(Integer mainId) {
        return HttpResult.ok(areaCalculationMainService.getBatchExecutionStatisticsVos(mainId));
    }

}
